<?php

namespace app\admin\controller;

use think\Controller;

class Data extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->admin = db('admin');
        $this->news_group = db('news_group');
        $this->news= db('news');
    }


    /**
     * 验证后台账号密码
     */
    public function dologin($name, $pwd)
    {
        $map['username'] = $name;
        $find = $this->admin->where($map)->find();
        if ($find) {
            if (md5(md5($pwd)) == $find['password']) {
                $return['status'] = 200;
                $return['mes'] = "验证通过,正在跳转...";
                $return['info'] = $find;
            } else {
                $return['status'] = 501;
                $return['mes'] = "密码错误";
            }
        } else {
            $return['status'] = 500;
            $return['mes'] = "账号不存在";
        }
        return $return;
    }

    public function group_add($add)
    {
        return $this->news_group->insert($add);
    }

    public function group_edit($arr, $type = "upda")
    {
        if ($type == "upda") {
            return $this->news_group->update($arr);
        } else {
            return $this->news_group->find($arr);
        }
    }

    public function group($page = 1, $limit = 15, $map = array())
    {
        $return['data'] = $this->news_group->where($map)->order("id desc")->page($page, $limit)->select();
        foreach ($return['data'] as $k => $v) {
            if ($v['updatime']) {
                $return['data'][$k]['upda'] = date("Y-m-d H:i", $v['updatime']);
            } else {
                $return['data'][$k]['upda'] = "暂无数据";
            }
        }
        $return['count'] = $this->news_group->where($map)->count();
        return $return;
    }

    public function group_delete($id)
    {
        return $this->news_group->delete($id);
    }


    public function add($add)
    {
        return $this->news->insert($add);
    }
}
